const db = require('../models');
const User = db.User;
const UserSercices = require('../services/userInfoService');

const BarTable = db.BarTable;
const BarActivity = db.BarActivity;
const Luck = db.Luck;

// 创建新用户
exports.createUser = async (req, res) => {
  try {
    const findResult = await UserSercices.findUserExists(req.body.wxUserAccount);
    if (findResult) {    
      const user = await User.create(req.body);
      res.status(200).send(user);
    } else {
      const updateUser = await User.update(req.body,{
        where: {
          wxUserAccount: req.body.wxUserAccount
        }
      });
      res.status(200).send("用户信息已更新");
    }
  } catch (error) {
    console.log(error);
    res.status(400).send(error);
  }
};
//测试接口，增加数据使用
exports.createTest = async (req, res) => {
  try {
    // const user = await BarTable.create(req.body);//酒吧桌子数据表创建数据
    // const user = await BarActivity.create(req.body);//酒吧活动数据表创建数据
    const user = await Luck.create(req.body);//彩头创建数据
    res.status(200).send(user);
  } catch (error) {
    res.status(400).send(error);
  }
};

// 获取某个id下的用户
exports.getIdUsers = async (req, res) => {
  const id = req.params.id;
  try {
    const user = await User.findOne({
      where: { id: id },
    });
    res.status(200).send(user);
  } catch (error) {
    res.status(500).send(error);
  }
};
